package com.offcn.project.controller;

import com.offcn.common.response.AppResponse;
import com.offcn.project.pojo.*;
import com.offcn.project.service.ProjectInfoService;
import com.offcn.project.vo.resp.ProjectDetailVo;
import com.offcn.project.vo.resp.ProjectVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Api(tags="项目的详细信息")
@RequestMapping("/projectInfo")
@RestController
public class ProjectInfoController {

    @Autowired
    private ProjectInfoService infoService;

    @ApiOperation("根据项目ID获取回报数据")
    @GetMapping("/getReturns/{projectId}")
    public AppResponse<List<TReturn>> getReturnList(@PathVariable("projectId") Integer projectId){
        List<TReturn> returnList = infoService.getReturnList(projectId);
        return AppResponse.ok(returnList);
    }

    @ApiOperation("根据回报ID获取回报数据")
    @GetMapping("/getReturn/{returnId}")
    public AppResponse<TReturn> getReturnById(@PathVariable("returnId") Integer returnId){
        TReturn tReturn = infoService.getReturnById(returnId);
        return AppResponse.ok(tReturn);
    }


    @ApiOperation("获取所有的项目数据")
    @GetMapping("/getAllProject")
    public AppResponse<List<ProjectVo>> getAllProject(){
        List<TProject> allProject = infoService.findAllProject();
        ArrayList<ProjectVo> pvos = new ArrayList<>();
        for(TProject pro : allProject){
            ProjectVo vo = new ProjectVo();
            BeanUtils.copyProperties(pro,vo);
            // 查询图片
            List<TProjectImages> images = infoService.getProjectImage(pro.getId());
            for(TProjectImages image : images){
                if(image.getImgtype() == 0){
                    vo.setHeaderImage(image.getImgurl());
                }
            }
            pvos.add(vo);
        }
        return AppResponse.ok(pvos);
    }


    @ApiOperation("获取项目的详细信息")
    @GetMapping("/getProjectById")
    public AppResponse<ProjectDetailVo> getProjectById(Integer projectId){
        ProjectDetailVo detailVo = new ProjectDetailVo();
        // 查询的project的数据
        TProject project = infoService.findOne(projectId);
        BeanUtils.copyProperties(project,detailVo);
        // 图片
        List<TProjectImages> projectImages = infoService.getProjectImage(projectId);
        // 详情图片的集合
        List detailsImage = new ArrayList();
        if(projectImages == null || projectImages.size() == 0){
            projectImages = new ArrayList<>();
        }
        for(TProjectImages image : projectImages){
            if(image.getImgtype() == 0){  // 头图片
                detailVo.setHeaderImage(image.getImgurl());
            }else{// 详情图片
                detailsImage.add(image.getImgurl());
            }
        }
        detailVo.setDetailsImage(detailsImage);
        // 回报
        List<TReturn> returnList = infoService.getReturnList(projectId);
        detailVo.setProjectReturns(returnList);

        return AppResponse.ok(detailVo);
    }

    @ApiOperation("获取所有的标签")
    @GetMapping("/findAllTag")
    public AppResponse<List<TTag>> findAllTag(){
        List<TTag> allTag = infoService.findAllTag();
        return AppResponse.ok(allTag);
    }

    @ApiOperation("获取所有的分类")
    @GetMapping("/findAllType")
    public AppResponse<List<TType>> findAllType(){
        List<TType> allType = infoService.findAllType();
        return AppResponse.ok(allType);
    }



}
